package com.ums.servicezuul.controller;

import com.ums.servicezuul.service.RefreshRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class RefreshController {

    @Autowired
    RefreshRouteService refreshRouteService;

    @Autowired
    ZuulHandlerMapping zuulHandlerMapping;

    @GetMapping("/refreshRoute")
    public String refresh() {
        refreshRouteService.refreshRoute();
        return "refresh success"+new Date();
    }

    @GetMapping("/watchRoute")
    public Map<String, Object> watchNowRoute() {
        //可以用debug模式看里面具体是什么
        Map<String, Object> handlerMap = zuulHandlerMapping.getHandlerMap();
        Set<String> stringSet = handlerMap.keySet();
        Iterator<String> iterator = stringSet.iterator();
        Map map = new HashMap();
        while (iterator.hasNext()) {
            String key = iterator.next();
            String value = handlerMap.get(key).toString();
            System.out.println("key = " + key+", value="+value);
            map.put(key, value);
        }
        //return handlerMap;
        return map;

    }
}
